package com.spring.boot.standard.admin.common;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SwaggerConfig {

    public SwaggerConfig() {
    }

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("authorization").description("平台统一维护的请求令牌")
                .defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJxaW5oYW8ifQ.uqI9ZMBTD8vX3R_btTDHC8CfuQ9CsRdqCjpycZHMwyY")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_12)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .globalOperationParameters(pars);
    }

//    @Bean
//    public Docket swaggerSpringMvcPlugin() {
//        ParameterBuilder ticketPar = new ParameterBuilder();
//        ticketPar.name("authorization").description("请求令牌").defaultValue("eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNocy1sKwyAQheG9zLMDE6NOzG68TCBtaYraUCjdezVvPx_nfOHWdlhhCsZ6TRED64yGMmFwKaNk2mJaEgs7UFDfsY91r73WXu24yxOrlFPKwNBgnezCZNjZWYF8XgM8-dleUI6HjKNj5ZWG3x8AAP__.UeJ39fvbcQcKFKVikUCN97BuKxX9hpa9HNsAm5S5Z4Ihv2i_XQmR-d0Cts0sWMJ-ZVumRuKxlw1ddqIZx5MS6g").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        List<Parameter> pars = new ArrayList();
//        pars.add(ticketPar.build());
//        return (new Docket(DocumentationType.SWAGGER_12)).useDefaultResponseMessages(false).apiInfo(this.apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build().globalOperationParameters(pars);
//    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("xxxx", "https://www.xxxx.com.cn", "qinhao@xxxx.com.cn");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("测试系统")
                .description("这是一个线上测试系统")
                .version("1.0.0")
                .termsOfServiceUrl("https://www.xxxx.com.cn")
                .contact(contact)
                .license("XXXX ALL RIGHT RESERVED")
                .licenseUrl("https://www.xxxx.com.cn")
                .build();
        return apiInfo;
    }

}
